package com.smasher.doc.ui;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.smasher.core.other.WeakReferenceHandler;
import com.smasher.core.thread.ThreadPool;
import com.smasher.doc.R;
import com.smasher.doc.entity.CacheEntity;
import com.smasher.doc.operation.WorkTaskReadRaw;
import com.smasher.doc.operation.WorkTaskReadUri;


public class DocActivity extends AppCompatActivity implements Handler.Callback {

    private final WeakReferenceHandler mHandler = new WeakReferenceHandler(this);
    private boolean isGenerating = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.setProperty("org.apache.poi.javax.xml.stream.XMLInputFactory", "com.fasterxml.aalto.stax.InputFactoryImpl");
        System.setProperty("org.apache.poi.javax.xml.stream.XMLOutputFactory", "com.fasterxml.aalto.stax.OutputFactoryImpl");
        System.setProperty("org.apache.poi.javax.xml.stream.XMLEventFactory", "com.fasterxml.aalto.stax.EventFactoryImpl");
        setContentView(R.layout.activity_doc_simple);
    }

    public void generate(View view) {
        //OperationReadHelper.readDocFromExternal(this, "test.docx");
        //OperationReadHelper.readDocFromRaw(this, R.raw.test);
        if (!isGenerating) {
            WorkTaskReadRaw task = new WorkTaskReadRaw(this, mHandler);
            ThreadPool.getInstance(ThreadPool.PRIORITY_HIGH).submit(task);
        } else {
            showToastIntelligent("正在生成中，请勿重复点击");
        }
    }

    public void selectDoc(View view) {
        mActivityResultLauncher.launch("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    }


    private void showToastIntelligent(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }


    /**
     * @param msg A {@link Message Message} object
     * @return True if no further handling is desired
     */
    @Override
    public boolean handleMessage(@NonNull Message msg) {
        int what = msg.what;
        if (what == WorkTaskReadUri.MSG_END) {
            isGenerating = false;
            showToastIntelligent("操作结束");
            if (msg.obj instanceof CacheEntity entity) {
                Gson gson = new Gson();
                String target = gson.toJson(entity);
                Log.e("CacheEntity", target);
            }

            return true;
        }
        return false;
    }

    private final ActivityResultLauncher<String> mActivityResultLauncher =
            registerForActivityResult(new ActivityResultContracts.GetContent(), uri -> {
                WorkTaskReadUri task = new WorkTaskReadUri(this, mHandler, uri);
                ThreadPool.getInstance(ThreadPool.PRIORITY_HIGH).submit(task);
            });
}
